import * as cacheUtils from '../../../../../model/CacheUtils';
const constants = require('../../../../../common/Constants.js');
const posUtil = require('../../commonPos.js');
const UmfDialog = require('../../../../../utils/UmfDialog.js');
import network_utils from '../../../../../http/NetworkUtils'
let terminalList = []
let cacheManager = null;
Page({
  /**
   * 页面的初始数据
   */

  data: {
    checkedArr: [],//选中的终端序列号--[序列号1，序列号2，序列号3]
    terminalNoArr: [], //展示的复选框
  },
  //  isContainElement(element, key){
  //   return element.indexOf(key)
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    var pages = getCurrentPages();
    // var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    
    this.setData({
      toolsNum: options.toolsNum,
      modelName: options.modelNames,
      itemIndex: options.itemIndex,
      prevPage: prevPage
    })
    cacheManager = new cacheUtils.cacheManager()
      .init("terminalPOSInfo" + options.storeId, constants.STORAGE_FUNC.TERMINALPOSINFO);
    let cacheStorage = cacheManager.getFunc();
    if (cacheStorage){
      this.setData({
        checkedArr: cacheStorage.terminalItem[options.itemIndex].serialArr ? cacheStorage.terminalItem[options.itemIndex].serialArr : [],
        // terminalNoArr: cacheStorage.terminalItem[options.itemIndex].serialArr ? cacheStorage.terminalItem[options.itemIndex].serialArr:[]
      })
    }   
    this.goSearch()
  },
  /**
   * 查看已选择的终端号
   */
  checkSelected: function () {
    this.setData({
      terminalNoArr: this.data.checkedArr
    })
  },
  /***
   * 模糊查询
   */
  fuzzySearch:function(e){
    this.goSearch()
    let searchText = e.detail.value.searchTerminal
    let helpers = {
      _codeFilter: (item) => {
        return item.value.indexOf(searchText) >= 0
      }
    };
    var searchArr = terminalList.filter(helpers._codeFilter)
    for(let i=0;i<searchArr.length;i++){
      for(let j=0;j<this.data.checkedArr.length;j++){
        if (searchArr[i].value == this.data.checkedArr[j].value) {
          searchArr[i].checked = true
        }
      }
    }
    this.setData({
      terminalNoArr:searchArr
    })
  },
  /**
     * 搜索
     */
  goSearch: function (e) {
    
    let params = {
      //TODO 模糊查询字段
      // searchText: 
      modelName: this.data.modelName
    }
    // TODO 网络请求接口  请求后的操作
    network_utils.buildRequest(constants.Interfaces.FUNC_GETSERIALS, params, {
      doHttpSucess: (response) => {
        if (constants.RetCodes.SUCCESS === response.retCode) {

          _formatData(this, response.list)
          return true;
        }
        return false;
      }
    }).send();
  },
  /**
   * 复选框事件
   */
  checkboxChange: function (e) {
    let checkedKeyArr = e.detail.value
    let checkedArrNow = this.data.checkedArr
    let terminalNoArrNow = this.data.terminalNoArr
    //无选中的终端号
    if (checkedKeyArr.length == 0) {
      for (let i = 0; i < terminalNoArrNow.length; i++) {
        if (terminalNoArrNow[i].value == checkedArrNow[0].value) {
          terminalNoArrNow[i].checked = false
        }
      }
      this.setData({
        terminalNoArr: terminalNoArrNow,
        checkedArr: []
      })
      this.data.prevPage._addTerminalModel({ "id": "serialArr", "current": this.data.itemIndex, "value": this.data.checkedArr });
      return
    }
    let id = checkedKeyArr.length - 1
    //选中终端号多于机具数量
    if (checkedKeyArr.length > this.data.toolsNum) {
      terminalNoArrNow[checkedKeyArr[id]].checked = false
      this.setData({
        terminalNoArr: terminalNoArrNow
      })
      UmfDialog.show("已选终端号的数量不应多于机具数量");
      return
    }
    checkedArrNow = []
    for(let i=0;i<terminalNoArrNow.length;i++){
      terminalNoArrNow[i].checked = false
    }
    for (let item = 0; item < checkedKeyArr.length; item++) {
      terminalNoArrNow[checkedKeyArr[item]].checked = true
      let checkItem = {
        key: item,
        value: terminalNoArrNow[checkedKeyArr[item]].value,
        checked: true
      }
      checkedArrNow.push(checkItem)
    }

    this.setData({
      checkedArr: checkedArrNow,
      terminalNoArr: terminalNoArrNow
    })
    this.data.prevPage._addTerminalModel({ "id": "serialArr", "current": this.data.itemIndex, "value": checkedArrNow });
  },

})



function _formatData(page, list) {
  let terminalNoArr = []
  if(!list){
    UmfDialog.show("没有可用的序列号，请联系上级代理商");
    return
  }
  if (list.length > 0) {
    for (let i = 0; i < list.length; i++) {
      let checked = false
      for(let j=0;j<page.data.checkedArr.length;j++){
        if (list[i].snNumber == page.data.checkedArr[j].value){
          checked = true
        }
      }     
      let snListItem = {
        key: i,
        value: list[i].snNumber,
        checked: checked
      }
      terminalNoArr.push(snListItem)
    }
  }
  terminalList = terminalNoArr
  page.setData({
    terminalNoArr: terminalNoArr
  });
}
